<?php

use think\Request;

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 记录行为日志，并执行该行为的规则
 * @param null $controller 控制器
 * @param null $content 内容
 * @param null $name 用户名
 * @param null $category 类型
 * @return string
 */
 
 
 
 
function action_log($content = null, $tabelAll = null, $category = 0, $username = null)
{
    //插入行为日志
    $request = Request::instance();
    $controller = $request->module() . '/' . $request->controller() . '/' . $request->action();
    $ip = $request->ip();
    $userinfo = cookie('username');
    $user = base64_decode($userinfo);
    $id = explode(',', $user);
    if (!isset($id[0])) {
        return 3;
    }
    if ($username == null || $username == '') {
        $name = $id[0];
    } else {
        $name = $username;
    }
    if ($category == 1) {
        $category = 'info';
    } else {
        $category = 'error';
    };
    $data['name'] = $name;
    $data['ip'] = $ip;
    $data['controller'] = $controller;
    $data['time'] = time();
    $data['category'] = $category;
    $data['content'] = $content;

    db($tabelAll)->insert($data);
}


/**
 * 记录行为日志，并执行该行为的规则
 * @param null $controller 控制器
 * @param null $content 内容
 * @param null $name 用户名
 * @param null $category 类型
 * @return string
 */
function action_log_index($content = null, $tabelAll = null, $category = 0, $username = null)
{
    //插入行为日志
    $request = Request::instance();
    $controller = $request->module() . '/' . $request->controller() . '/' . $request->action();
    $ip = $request->ip();
    $userinfo = cookie('userinfo');
    $user = base64_decode($userinfo);
    $id = explode(',', $user);
    if (!isset($id[1])) {
        return 3;
    }
    if ($username == null || $username == '') {
        $name = urldecode($id[1]);
    } else {
        $name = $username;
    }
    if ($category == 1) {
        $category = 'info';
    } else {
        $category = 'error';
    };
    $data['name'] = $name;
    $data['ip'] = $ip;
    $data['controller'] = $controller;
    $data['time'] = time();
    $data['category'] = $category;
    $data['content'] = $content;

    db($tabelAll)->insert($data);
}

/**
*部分权限认证
*/
function auth()
{
    $auth = new \auth\Auth();
    $request = Request::instance();
    $name = $request->module() . '/' . $request->controller() . '/' . $request->action();
    $userinfo = cookie('username');
    $user = base64_decode($userinfo);
    $id = explode(',', $user);
    if (!isset($id[1])) {
		return 3;
    } else {
        $user = db('user')->where('uid', $id[1])->find();
        if ($user['r_id'] == 1) {
            return 2;
        } else if (!$auth->check($name, $user['uid'])) {
            /* return array('status'=>'error','msg'=>'有权限！');*/
            //            $this->redirect('error/index');
            //            $this->error('没有权限');
            return 1;
        } else {
            return 2;
        }
    }
}


